package handler.assetFormHandlers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import customGUI.selectors.AssetSelector;


import checker.AddAssetFormChecker;
import checker.AssetFormChecker;
import db.Asset;
import db.DBConnector;

import form.AssetForm.AssetForm;

public class AFEditButtonActionListener extends AbstractAFErrorCheckingButtonListener{


	public AFEditButtonActionListener(AssetForm assetForm){
		super(assetForm, new AddAssetFormChecker());
	}
	
	@Override
	public void performOperation() {
		if(assetForm!= null && assetForm.getSelector()!=null && assetForm.getSelector().getSelectedID()!=-1){
			Asset uneditedAsset = Asset.createAssetFromID(assetForm.getSelector().getSelectedID());
			Asset newAsset = uneditedAsset.getCopy();		
			assetForm.updateAssetInfo(newAsset);
	
			uneditedAsset.updateSelfInDB(newAsset);	
			assetForm.getSelector().refreshAssetChoices();
			JOptionPane.showMessageDialog(null, "Successfuly edited "+newAsset.getName(), "Success!", JOptionPane.INFORMATION_MESSAGE);	
		}
	}
}
